package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PrimaryKeyIdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.customer.api.domain.CustomerManagement;
import com.ruoyi.mall.api.domian.Product;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.enums.OrderReturnStatusEnum;
import com.ruoyi.mall.enums.OrderStatusEnum;
import com.ruoyi.mall.enums.PaymentStatusENum;
import com.ruoyi.mall.mapper.*;
import com.ruoyi.mall.mapstruct.OrderProductMapper;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.service.IOrderReturnService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 退货Service业务层处理
 * 
 * @author luogongquan
 * @date 2024-10-25
 */
@Service
public class OrderReturnServiceImpl implements IOrderReturnService 
{
    @Autowired
    private OrderReturnMapper orderReturnMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderProductAssociationMapper orderProductAssociationMapper;
    @Autowired
    private OrderReturnDetailMapper orderReturnDetailMapper;
    @Autowired
    private PaymentMapper paymentMapper;

    /**
     * 查询退货
     * 
     * @param id 退货主键
     * @return 退货
     */
    @Override
    public OrderReturn selectOrderReturnById(Long id)
    {
        return orderReturnMapper.selectOrderReturnById(id);
    }

    /**
     * 查询退货列表
     * 
     * @param orderReturn 退货
     * @return 退货
     */
    @Override
    public List<OrderReturn> selectOrderReturnList(OrderReturn orderReturn)
    {
        return orderReturnMapper.selectOrderReturnList(orderReturn);
    }

    /**
     * 新增退货
     * 
     * @param orderReturn 退货
     * @return 结果
     */
    @Override
    public int insertOrderReturn(OrderReturn orderReturn)
    {
        orderReturn.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        orderReturn.setCreateTime(DateUtils.getNowDate());
        return orderReturnMapper.insertOrderReturn(orderReturn);
    }

    /**
     * 修改退货
     * 
     * @param orderReturn 退货
     * @return 结果
     */
    @Override
    public int updateOrderReturn(OrderReturn orderReturn)
    {
        orderReturn.setUpdateTime(DateUtils.getNowDate());
        return orderReturnMapper.updateOrderReturn(orderReturn);
    }

    /**
     * 批量删除退货
     * 
     * @param ids 需要删除的退货主键
     * @return 结果
     */
    @Override
    public int deleteOrderReturnByIds(Long[] ids)
    {
        return orderReturnMapper.deleteOrderReturnByIds(ids);
    }

    /**
     * 删除退货信息
     * 
     * @param id 退货主键
     * @return 结果
     */
    @Override
    public int deleteOrderReturnById(Long id)
    {
        return orderReturnMapper.deleteOrderReturnById(id);
    }

    @Override
    @Transactional
    public Boolean apply4Refund(OrderReturn orderReturn) {
        Orders orders = ordersMapper.selectOrdersById(orderReturn.getOrderId());
        CustomerManagement customerManagement = SecurityUtils.getLoginUser().getCustomerManagement();
        orderReturn.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        orderReturn.setDeptId(orders.getDeptId());
        orderReturn.setReturnAmount(orders.getAmount());
        orderReturn.setReturnName(customerManagement.getRealName());
        orderReturn.setReturnPhone(customerManagement.getPhoneNumber());
        orderReturn.setStatus(OrderReturnStatusEnum.AUDIT.getCode());
        orderReturn.setCreateBy(SecurityUtils.getUsername());
        orderReturn.setCreateTime(DateUtils.getNowDate());
        orderReturnMapper.insertOrderReturn(orderReturn);

        //更新订单状态为退款中
        Orders orders1 = new Orders();
        orders1.setStatus(OrderStatusEnum.REFUND.getCode());
        orders1.setId(orders.getId());
        orders1.setUpdateTime(DateUtils.getNowDate());
        orders1.setUpdateBy(SecurityUtils.getUsername());
        ordersMapper.updateOrders(orders1);

        //获取订单商品
        List<OrderProductAssociation> orderProductAssociations = orderProductAssociationMapper.selectOrderProductAssociationByOrderId(orderReturn.getOrderId());
        List<OrderReturnDetail> orderReturnDetails = OrderProductMapper.INSTANCE.OrderProductAssociation2OrderReturns(orderProductAssociations);
        orderReturnDetails.forEach(item->{
            item.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
            item.setOrderReturnId(orderReturn.getId());
        });
        orderReturnDetailMapper.batchInsertOrderReturnDetail(orderReturnDetails);
        return true;
    }

    @Override
    @Transactional
    public Boolean refuseRefund(OrderReturn orderReturn) {
        Date nowDate = DateUtils.getNowDate();
        orderReturn.setStatus(OrderReturnStatusEnum.REFUND_REFUSED.getCode());
        orderReturn.setUpdateBy(SecurityUtils.getUsername());
        orderReturn.setUpdateTime(nowDate);
        orderReturn.setHandleMan(SecurityUtils.getUsername());
        orderReturn.setHandleTime(nowDate);
        orderReturnMapper.updateOrderReturn(orderReturn);

        Orders orders = new Orders();
        orders.setId(orderReturn.getOrderId());
        orders.setStatus(OrderStatusEnum.PAID.getCode());
        orders.setUpdateBy(SecurityUtils.getUsername());
        orders.setUpdateTime(nowDate);
        ordersMapper.updateOrders(orders);
        return true;
    }

    @Override
    @Transactional
    public Boolean sureRefund(OrderReturn orderReturn) {
        Date nowDate = DateUtils.getNowDate();
        orderReturn.setStatus(OrderReturnStatusEnum.CONFIRM_THE_REFUND.getCode());
        orderReturn.setUpdateBy(SecurityUtils.getUsername());
        orderReturn.setUpdateTime(nowDate);
        orderReturn.setHandleMan(SecurityUtils.getUsername());
        orderReturn.setHandleTime(nowDate);
        orderReturnMapper.updateOrderReturn(orderReturn);

        Orders orders = new Orders();
        orders.setId(orderReturn.getOrderId());
        orders.setStatus(OrderStatusEnum.REFUNDED.getCode());
        orders.setUpdateBy(SecurityUtils.getUsername());
        orders.setUpdateTime(nowDate);
        ordersMapper.updateOrders(orders);

        //生成退款的支付流水记录
        Payment payment = new Payment();
        payment.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        payment.setOrderId(orderReturn.getOrderId());
        payment.setOrderReturnId(orderReturn.getId());
        payment.setAmount(orderReturn.getReturnAmount());
        payment.setPaymentStatus(PaymentStatusENum.REFUND.getCode());
        payment.setCreateTime(nowDate);
        paymentMapper.insertPayment(payment);

        //通知退款


        return true;
    }
}
